var pattern = new RegExp(/\?Code=(\S*)/g);
var idx = pattern.exec(window.location.href);
var options = '', PictureCode, Content_PC = '', Content_M = '';

var Category = $('select[data-field="ItemCategory"]'),
    ItemCode = $('input[data-field="ItemCode"]'),
    ItemName = $('input[data-field="ItemName"]'),
    Price = $('input[data-field="ItemPrice"]'),
    OnHand = $('input[data-field="ItemOnHand"]');

$(function(){
    requestSort();
    requestAjax();
    $('.u_pictures .u_item, .u_pictures .u_add_item').on('click', addPictures);
    $("li[data-opt='add']").on('click', addCode);
    $("li[data-opt='back']").on('click', back);    
    setContent('iframePC', Content_PC);
    setContent('iframeM', Content_M);
    $("#js_submit, li[data-opt='save']").on("click", saveSubmit);
});

function requestSort(){
    var postData01 = Object.create(postData);
    postData01.Content = {
        'CodeList': [
            'MDM0022'
        ]
    };
    $.ajax({
        async: false,
        type: 'post',
        url: mdmPath + '/MDMCommonality/MDMCommonality01',
        data: postData01,
        dataType: 'json',
        success: function (re) {
            if (re.Status == 200) {
                //console.log(re.Content.DropdownData.MDM0011);
                var MDM0022 = re.Content.DropdownData.MDM0022;
                for (var i in MDM0022) {
                    options += '<option value="' + i + '">' + MDM0022[i] + '</option>';
                }
                Category.html(options);
            } else {
                myAlert(re.Message);
            }
        }
    });
}

function requestAjax(){ //MDM002303
    if(idx){
        var _postData = Object.create(postData);    //请求头
        _postData.Method = 'Q';
        _postData.Content = {
            "Code": idx[1]
        }
        $.ajax({
            async: false,
            type: 'post', 
            url: mdmPath + '/MDM0023/MDM002303',
            data: _postData,
            dataType: 'json',
            success: function (re) {
                loadAjax(re);
            }
        });        
    }    
}
function loadAjax(re){
    if(re.Status==200){
        Category.val(re.Content.Category);  //分类
        ItemCode.val(re.Content.Code);  //单号
        ItemName.val(re.Content.Name);    //标题
        Price.val(re.Content.Price);    //积分
        OnHand.val(re.Content.OnHand);  //库存
        Content_PC = re.Content.Content_PC;
        Content_M = re.Content.Content_M;
        if(re.Content.PictureLocation){  //PC图片
            PictureCode = re.Content.PictureCode;
            $("li[data-tab='detailPC'] .u_item").html('<img src="'+PicAddress+re.Content.PictureLocation+'">');
        }
        MDM002302 = re.Content.MDM002302;
        if(MDM002302){
            var html = '';
            MDM002302.forEach(function(v,i,a){
                html += '<li class="u_add_item">'+
                    '<img src="'+PicAddress+v.Piclocation+'" alt="">'+
                    '<i class="fa fa-times fa-2x u_close"></i>'+
                    '<p class="u_desc">'+v.PicName+'</p>'+
                '</li>'
            });
            $('li[data-tab="detailPC"] .u_add_item').before(html);
            var count = MDM002302.length;
            console.log(count)
            $('.u_add_item .u_close').unbind('click').bind('click', function(){
                $(this).parents().filter('.u_add_item').remove();
                count > 4 &&(count = 4);
                count --;
            });
        }
        $("#createDate").val(re.Content.CreateDate);    //
        $("#updateDate").val(re.Content.UpdateDate);    //
        $("#createUser").val(re.Content.CreateUser);    //
        $("#updateUser").val(re.Content.UpdateUser);    //
    } else if (re.Status == -999) {
        alert_QX($('body'), re.Message);
    } else {
        myAlert(re.Message);
    }
}

//Options
function addCode(){
    window.open('MDM002302.html', '_blank');  
}
function back(){
    window.open('MDM002301.html', '_self');
}
function saveSubmit(){  //MDM002301
    var PictureCode = "", PictureLocation = "";
    if($("li[data-tab='detailPC'] .u_item img").length>0){  //如果img存在，获取img地址
        var src_P = $("li[data-tab='detailPC'] .u_item img")[0].src;
        if(src_P.indexOf(PicAddress)!=-1){
            PictureLocation = src_P.slice(PicAddress.length);
            var tempArr = PictureLocation.split('/');
            var tempCode = tempArr[tempArr.length-1];
            PictureCode = tempCode.split('.')[0];
        }
    }
    var PicCode= "", PicName = "", MDM002302 = [];
    var src_Ps = $("li[data-tab='detailPC'] .u_add_item").find("img");
    if(src_Ps.length>0){
        var txt_Ps = $("li[data-tab='detailPC'] .u_add_item").find("p");
        for(var i=0;i<src_Ps.length;i++){
            var Piclocation = src_Ps[i].src.slice(PicAddress.length);
            var tempArr = Piclocation.split('/');
            var tempCode = tempArr[tempArr.length-1];
            PicCode = tempCode.split('.')[0];
            PicName = txt_Ps.eq(i).html();
            MDM002302.push({Code:ItemCode.val(),LineID:i,OrderById:i,PicCode:PicCode,PicName:PicName,Piclocation:Piclocation});
        }
    }
    var requestData = {
        Content: {
            Category: Category.val(),
            Code: ItemCode.val(),    
            Name: ItemName.val(),
            Price: Price.val(),
            OnHand: OnHand.val(),
            Content_PC: getContent('iframePC'),
            Content_M: getContent('iframeM'),
            PictureCode: PictureCode,
            PictureLocation: PictureLocation,
            MDM002302: MDM002302
        },
    }
    if(idx){
        requestData.Method = 'U'
    }else{
        requestData.Method = 'A'
    }
    for(i in postData){
        requestData[i] = postData[i]
    }
    console.log(requestData);
    $.ajax({
        type: "post",
        url: mdmPath + '/MDM0023/MDM002301',
        data: requestData,
        dataType: "json",
        success: function(re) {
            if(re.Status==200){
                myAlert('保存成功')
            }else{
                myAlert(re.Message)
            }
        }
    });
}

